<template>
  <view class="container">
    <!-- 顶部搜索栏 -->
    <view class="search-bar">
      <view class="back-btn" @click="goBack">×</view>
      <view class="tab-group">
        <view
            class="tab"
            :class="{ active: activeTab === 'secondHand' }"
            @click="switchTab('secondHand')"
        >二手房</view>
        <view
            class="tab"
            :class="{ active: activeTab === 'newHouse' }"
            @click="switchTab('newHouse')"
        >新房</view>
      </view>
      <view class="search-icon" @click="searchHouse"></view>
    </view>

    <!-- 筛选栏 -->
    <!-- 筛选栏优化部分 -->
    <view class="filter-bar" v-permission="[]">
      <view class="filter-item">
        <text class="filter-text">祥云县</text>
        <image src="https://via.placeholder.com/20x20" mode="aspectFit" class="filter-icon"></image>
      </view>
      <view class="filter-item">
        <text class="filter-text">价格</text>
        <image src="https://via.placeholder.com/20x20" mode="aspectFit" class="filter-icon"></image>
      </view>
      <view class="filter-item">
        <text class="filter-text">房型</text>
        <image src="https://via.placeholder.com/20x20" mode="aspectFit" class="filter-icon"></image>
      </view>
      <view class="filter-item">
        <text class="filter-text">多选</text>
        <image src="https://via.placeholder.com/20x20" mode="aspectFit" class="filter-icon"></image>
      </view>
    </view>

    <!-- 地图组件 -->
    <map
        :longitude="mapCenter.longitude"
        :latitude="mapCenter.latitude"
        :scale="10"
        :markers="markers"
        @markertap="handleMarkerTap"
        style="width: 100%; height: calc(100vh - 300rpx);"
    ></map>

    <!-- 右侧按钮 -->
    <view class="right-btns">
      <view class="btn" @click="handleEvaluate">评价</view>
      <view class="btn" @click="handleLayer">图层</view>
      <view class="btn" @click="handleFavorite">关注</view>
      <view class="btn locate-btn" @click="locate">定位</view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      activeTab: 'newHouse',
      mapCenter: {
        longitude: 114.9423902,
        latitude: 28.2399297
      },
      markers: [
        {
          id: 1,
          longitude: 114.9423902,
          latitude: 28.2399297,
          title: '上高市--66个',
          iconPath: '/static/empty/default.png',
          width: 120,
          height: 80,
          callout: {
            content: '上高市\n66个',
            fontSize: 22,
            borderRadius: 8,
            borderWidth: 1,
            padding: 8,
            display: 'ALWAYS'
          }
        },
        {
          id: 2,
          longitude: 114.4458,
          latitude: 28.10656,
          title: '万载县\n2个',
          iconPath: '/static/empty/default.png',
          width: 100,
          height: 70,
          callout: {
            content: '万载县\n2个',
            fontSize: 28,
            borderRadius: 8,
            borderWidth: 1,
            padding: 8,
            display: 'ALWAYS'
          }
        },
        {
          id: 3,
          longitude: 114.409427,
          latitude: 27.884782,
          title: '金开区\n2个',
          iconPath: '/static/empty/default.png',
          width: 100,
          height: 70,
          callout: {
            content: '金开区\n2个',
            fontSize: 28,
            borderRadius: 8,
            borderWidth: 1,
            padding: 8,
            display: 'ALWAYS'
          }
        }
      ]
    };
  },
  methods: {
    goBack() {
      uni.navigateBack();
    },
    switchTab(type) {
      this.activeTab = type;
    },
    searchHouse() {
      uni.showToast({ title: '搜索功能开发中', icon: 'none' });
    },
    handleMarkerTap(e) {
      const marker = this.markers.find(m => m.id === e.markerId);
      if (marker) {
        uni.showToast({
          title: `点击${marker.title}`,
          icon: 'none'
        });
      } else {
        console.error('未找到对应的标记点数据');
      }
    //  uni.showToast({ title: `点击${marker.title}`, icon: 'none' });
    },
    locate() {
      uni.getLocation({
        type: 'gcj02',
        success: (res) => {
          this.mapCenter.longitude = res.longitude;
          this.mapCenter.latitude = res.latitude;
        }
      });
    },
    handleEvaluate() {
      uni.showToast({ title: '评价功能开发中', icon: 'none' });
    },
    handleLayer() {
      uni.showToast({ title: '图层功能开发中', icon: 'none' });
    },
    handleFavorite() {
      uni.showToast({ title: '关注功能开发中', icon: 'none' });
    }
  }
};
</script>

<style>
.container {
  position: relative;
}
.search-bar {
  display: flex;
  align-items: center;
  padding: 20rpx;
  background: #fff;
  box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.1);
}
.back-btn {
  font-size: 32rpx;
  margin-right: 20rpx;
}
.tab-group {
  flex: 1;
  display: flex;
  justify-content: center;
}
.tab {
  font-size: 30rpx;
  padding: 0 20rpx;
}
.tab.active {
  color: #007AFF;
  border-bottom: 2rpx solid #007AFF;
}
.search-icon {
  font-size: 32rpx;
}
.filter-bar {
  display: flex;
  justify-content: space-around;
  padding: 12rpx 20rpx;
  background: #f8f9fa;
  margin: 10rpx;
  border-radius: 12rpx;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.08);
}

.filter-item {
  display: flex;
  align-items: center;
  gap: 8rpx;
  padding: 8rpx 15rpx;
  border-radius: 8rpx;
}

.filter-text {
  font-size: 28rpx;
  color: #333;
}

.filter-icon {
  width: 24rpx;
  height: 24rpx;
}


.right-btns {
  position: fixed;
  right: 20rpx;
  top: 50%;
  transform: translateY(-50%);
}
.btn {
  width: 80rpx;
  height: 80rpx;
  background: #fff;
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 10rpx 0;
  border-radius: 10rpx;
  box-shadow: 0 2rpx 5rpx rgba(0,0,0,0.1);
}
.locate-btn {
  background: #007AFF;
  color: #fff;
}
</style>